package cn.flying.cloud.system.api.dubbo;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.util.CollectionUtils;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.common.enums.StatusEnum;
import cn.flying.cloud.system.api.UserServiceApi;
import cn.flying.cloud.system.model.Organization;
import cn.flying.cloud.system.model.Position;
import cn.flying.cloud.system.model.Role;
import cn.flying.cloud.system.model.User;
import cn.flying.cloud.system.org.entity.SysOrgInfo;
import cn.flying.cloud.system.org.service.SysOrgInfoService;
import cn.flying.cloud.system.position.entity.SysPostInfo;
import cn.flying.cloud.system.position.service.SysPostInfoService;
import cn.flying.cloud.system.role.entity.SysRoleInfo;
import cn.flying.cloud.system.role.service.SysRoleInfoService;
import cn.flying.cloud.system.user.entity.SysUserInfo;
import cn.flying.cloud.system.user.service.SysUserInfoService;
import cn.flying.cloud.system.vo.OrgInfoVO;

/**
 * 系统用户信息服务接口
 *
 * @author: admin
 * @date: 2025年03月28日 09:29
 * @version: 1.0
 */
@DubboService
public class UserApiService implements UserServiceApi {

    @Resource
    private SysUserInfoService userInfoService;
    @Resource
    private SysPostInfoService postInfoService;
    @Resource
    private SysOrgInfoService orgInfoService;
    @Resource
    private SysRoleInfoService roleInfoService;


    /**
     * 根据userId称查询用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public User findUserByUserId(String userId) {
        // 用户基本信息
        SysUserInfo info = new SysUserInfo();
        info.setUserId(userId);
        info = userInfoService.getOneObj(info);
        User user = new User();
        Ft.BeanCopier.copy(info, user);

        // 所属组织
        SysOrgInfo orgInfo = new SysOrgInfo();
        orgInfo.setOrgId(info.getOrgId());
        orgInfo = orgInfoService.getOneObj(orgInfo);
        Organization org = new Organization();
        Ft.BeanCopier.copy(orgInfo, org);
        user.setOrganization(org);

        // 拥有岗位
        if (Ft.String.isNotBlank(info.getPostId())) {
            String[] split = info.getPostId().split(",");
            Set<Position> positions = new HashSet<>();
            user.setPositions(positions);
            Arrays.stream(split).distinct().forEach(p -> {
                SysPostInfo postInfo = new SysPostInfo();
                postInfo.setPostId(p);
                postInfo = postInfoService.getOneObj(postInfo);
                Position position = new Position();
                Ft.BeanCopier.copy(postInfo, position);

                positions.add(position);
            });
        }

        // 拥有角色
        if (Ft.String.isNotBlank(info.getRoleId())) {
            String[] split = info.getRoleId().split(",");
            Set<Role> roles = new HashSet<>();
            user.setRoles(roles);
            Arrays.stream(split).distinct().forEach(p -> {
                SysRoleInfo roleInfo = new SysRoleInfo();
                roleInfo.setRoleId(p);
                roleInfo = roleInfoService.getOneObj(roleInfo);
                Role role = new Role();
                Ft.BeanCopier.copy(roleInfo, role);

                roles.add(role);
            });
        }

        return user;
    }

    /**
     * 根据用户名称查询用户信息
     *
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {
        SysUserInfo info = new SysUserInfo();
        info.setUsername(username);
        info = userInfoService.getOneObj(info);
        User user = new User();
        Ft.BeanCopier.copy(info, user);

        // 所属组织
        SysOrgInfo orgInfo = new SysOrgInfo();
        orgInfo.setOrgId(info.getOrgId());
        orgInfo = orgInfoService.getOneObj(orgInfo);
        Organization org = new Organization();
        Ft.BeanCopier.copy(orgInfo, org);
        user.setOrganization(org);

        // 拥有岗位
        if (Ft.String.isNotBlank(info.getPostId())) {
            String[] split = info.getPostId().split(",");
            Set<Position> positions = new HashSet<>();
            user.setPositions(positions);
            Arrays.stream(split).distinct().forEach(p -> {
                SysPostInfo postInfo = new SysPostInfo();
                postInfo.setPostId(p);
                postInfo = postInfoService.getOneObj(postInfo);
                Position position = new Position();
                Ft.BeanCopier.copy(postInfo, position);

                positions.add(position);
            });
        }

        // 拥有角色
        if (Ft.String.isNotBlank(info.getRoleId())) {
            String[] split = info.getRoleId().split(",");
            Set<Role> roles = new HashSet<>();
            user.setRoles(roles);
            Arrays.stream(split).distinct().forEach(p -> {
                SysRoleInfo roleInfo = new SysRoleInfo();
                roleInfo.setRoleId(p);
                roleInfo = roleInfoService.getOneObj(roleInfo);
                Role role = new Role();
                Ft.BeanCopier.copy(roleInfo, role);

                roles.add(role);
            });
        }
        return user;
    }

    /**
     * 根据岗位ID查询岗位信息
     *
     * @param postId
     * @return
     */
    @Override
    public Position findPositionByPostId(String postId) {
        SysPostInfo postInfo = new SysPostInfo();
        postInfo.setPostId(postId);
        postInfo = postInfoService.getOneObj(postInfo);
        Position position = new Position();
        Ft.BeanCopier.copy(postInfo, position);

        List<SysUserInfo> userInfos = postInfoService.getUserByPostId(postId);
        if (!CollectionUtils.isEmpty(userInfos)) {
            Set<User> users = userInfos.stream().map(userInfo -> {
                User user = new User();
                Ft.BeanCopier.copy(userInfo, user);
                return user;
            }).collect(Collectors.toSet());
            position.setUsers(users);
        }

        return position;
    }

    /**
     * 根据岗位查询岗位信息
     *
     * @param postIds
     * @return
     */
    @Override
    public List<Position> findPositionByPostIds(List<String> postIds) {
        List<Position> list = new ArrayList<>();
        for (String postId : postIds) {
            Position position = findPositionByPostId(postId);
            list.add(position);
        }
        return list;
    }

    /**
     * 根据角色ID查询角色信息
     *
     * @param roleId
     * @return
     */
    @Override
    public Role findRoleByRoleId(String roleId) {
        SysRoleInfo roleInfo = new SysRoleInfo();
        roleInfo.setRoleId(roleId);
        roleInfo = roleInfoService.getOneObj(roleInfo);
        Role role = new Role();
        Ft.BeanCopier.copy(roleInfo, role);

        List<SysUserInfo> userInfos = roleInfoService.getUserByRoleId(roleId);
        if (!CollectionUtils.isEmpty(userInfos)) {
            Set<User> users = userInfos.stream().map(userInfo -> {
                User user = new User();
                Ft.BeanCopier.copy(userInfo, user);
                return user;
            }).collect(Collectors.toSet());
            role.setUsers(users);
        }

        return role;
    }

    /**
     * 根据角色查询角色信息
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<Role> findRoleByRoleIds(List<String> roleIds) {
        List<Role> list = new ArrayList<>();
        for (String roleId : roleIds) {
            Role role = findRoleByRoleId(roleId);
            list.add(role);
        }
        return list;
    }

    /**
     * 查询所有的组织机构
     *
     * @return
     */
    @Override
    public List<Organization> getAllOrganizationList() {
        SysOrgInfo orgInfo = new SysOrgInfo();
        orgInfo.setStatus(StatusEnum.STATUS_Y.getCode());
        List<OrgInfoVO> list = orgInfoService.getList(orgInfo);
        return list.stream().map(o -> {
            Organization organization = new Organization();
            Ft.BeanCopier.copy(o, organization);

            return organization;
        }).collect(Collectors.toList());
    }

    /**
     * 根据组织编码查询组织机构信息
     *
     * @param orgId
     * @return
     */
    @Override
    public Organization getOrganizationByOrgId(String orgId) {
        SysOrgInfo orgInfo = new SysOrgInfo();
        orgInfo.setOrgId(orgId);
        orgInfo = orgInfoService.getOneObj(orgInfo);
        Organization org = new Organization();
        Ft.BeanCopier.copy(orgInfo, org);

        SysUserInfo userInfo = new SysUserInfo();
        userInfo.setOrgId(orgId);
        userInfo.setStatus(StatusEnum.STATUS_Y.getCode());
        List<SysUserInfo> userInfos = userInfoService.getList(userInfo);
        if (!CollectionUtils.isEmpty(userInfos)) {
            Set<User> users = userInfos.stream().map(info -> {
                User user = new User();
                Ft.BeanCopier.copy(info, user);
                return user;
            }).collect(Collectors.toSet());
            org.setUsers(users);
        }

        return org;
    }
}
